<?php 
namespace xfyzf\api\controller;
use lib\PluginScanner;
class plugins{
    public static function loadPlugin($pluginName, $methodName = ''){
        if (class_exists("xfyzf\\plugins\\$pluginName\\controller\\Index")) {
            
            $pluginClass = "xfyzf\\plugins\\$pluginName\\controller\\Index";
           
            $apiInstance = new $pluginClass();
            if (method_exists($apiInstance, $methodName)) {
                define('pluginsassets', "/Core/class/xfyzf/plugins/$pluginName/assets/");
                define('pluginstem', ROOT_CLASS . "xfyzf/plugins/$pluginName/view/");
                define('pluginsauthfile', SYSTEM_ROOT . "auth/");
                
                if(!property_exists($apiInstance, 'info'))return false;
                if (array_key_exists('auth', $apiInstance->info) && array_key_exists('pwd', $apiInstance->info) && array_key_exists('path', $apiInstance->info) && self::prikeyauth($apiInstance->priKey) && $methodName != 'Token_effect')self::authxy($apiInstance->priKey,$apiInstance->info['pwd'],$apiInstance->info['path'],$apiInstance->info['auth']);
                $apiInstance->$methodName();
                return true; // 插件加载成功
            }
        }
        return false; // 插件加载失败
    }
    public static function authxy($priKey,$key,$file,$send = false){
        return PluginScanner::authxy($priKey,$key,$file,$send);
    }
    
    public static function prikeyauth($prikey = '')
    {
        try {
            if (empty($prikey)) {
                return false; // 如果私钥为空，返回 false
            }
    
            $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
                wordwrap($prikey, 64, "\n", true) .
                "\n-----END RSA PRIVATE KEY-----";
    
            // 尝试创建一个新的私钥资源，如果成功则返回 true
            $privateKey = openssl_pkey_get_private($res);
            if ($privateKey === false) {
                return false;
            }
    
            // 释放私钥资源
            openssl_free_key($privateKey);
    
            return true; // 如果一切正常，返回 true
        } catch (Exception $e) {
            // 如果发生任何异常，返回 false
            return false;
        }
    }
    
    
}


?>